package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/hello")
    public List<User> hello(){

        return userService.findAll();
    }
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
       String token = userService.login(user);
        if(token == null || token.length() == 0 ){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }
    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){


       return SysResult.success(userService.getUserList(pageResult));
    }
    /**请求路径 /user/status/{id}/{status}
     请求类型 PUT
     请求参数: 用户ID/状态值数据
     */
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){
        userService.updateStatus(user);
        return SysResult.success();
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }
    @GetMapping("/{id}")
    public SysResult findUserById(@PathVariable Integer id){
      User user =  userService.findUserById(id);
      return SysResult.success(user);
    }
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }
    /**
     * 请求路径: /user/{id}
     * 请求类型: delete
     * 请求参数
     * */
    @DeleteMapping("/{id}")
    public SysResult MedeleteById(@PathVariable Integer id){
        userService.MedeleteById(id);
        return SysResult.success();
    }
}
